﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Admin
{
    public partial class ProductEdit : BasePage
    {
        private ProductLogic productLogic = null;
        private CatalogueLogic catalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitCatalogueDDL();
            EditStateEnum action = GetAction();
            if (action == EditStateEnum.New)
            {
                this.Title = "添加商品";
            }
            else
            {
                this.Title = "修改商品";
            }

            if (!IsPostBack)
            {
                InitProduct(action);
            }
        }

        /// <summary>
        /// 载入栏目选择框
        /// </summary>
        private void InitCatalogueDDL()
        {
            this.SelectCatalogueDropDownList1.CatalogueLogicProvider = catalogueLogic;
            this.SelectCatalogueDropDownList1.CanClickNodeType = CatalogueTypeEnum.ProductList;
        }

        /// <summary>
        /// 载入待编辑产品信息
        /// </summary>
        private void InitProduct(EditStateEnum action)
        {
            try
            {
                if (action == EditStateEnum.Edit)
                {
                    string strId = Request.QueryString["id"];
                    long id = 0;
                    if (long.TryParse(strId, out id))
                    {
                        btnSave.CommandName = EditStateEnum.Edit.ToString();
                        btnSave.CommandArgument = id.ToString();

                        Product product = productLogic.GetById(id);
                        if (product != null)
                        {
                            ProductNameTextBox.Text = product.ProductName;
                            ProductCodeTextBox.Text = product.ProductCode;
                            SelectCatalogueDropDownList1.SelectedCatalogueId = product.CatalogueId;
                            lblNotice.Visible = true;
                            IsRecommendCheckBox.Checked = product.IsRecommend;
                            ProductContentFCKeditor.Text = product.ProductContent;
                        }
                        else
                        {
                            AddScript("showDialog('找不到该产品！');");
                        }
                    }
                    else
                    {
                        AddScript("showDialog('参数传递错误！');");
                    }
                }
                else
                {
                    btnSave.CommandName = EditStateEnum.New.ToString();
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate("ProductEditValidationGroup");

            if (!Page.IsValid)
            {
                return;
            }

            try
            {
                Product product = GetEditingProduct();
                if (product != null)
                {
                    productLogic.Save(product);
                    AddScript("showDialog('保存产品信息成功！');");
                }
                else
                {
                    AddScript("showDialog('要修改的产品信息已经不存在！');");
                }

                if (btnSave.CommandName == EditStateEnum.New.ToString())
                {
                    lblNotice.Visible = false;
                    ClearControlText(ProductNameTextBox, ProductCodeTextBox, IsRecommendCheckBox, ProductContentFCKeditor);
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("showDialog('保存产品信息失败！');");
            }
        }

        private Product GetEditingProduct()
        {
            Product product = null;

            if (btnSave.CommandName == EditStateEnum.Edit.ToString())
            {
                long id = long.Parse(btnSave.CommandArgument);
                product = productLogic.GetById(id);
            }
            else
            {
                product = new Product();
                product.Id = productLogic.GetNewIdentity();
                product.CreatedTime = DateTime.Now;
            }

            if (product != null)
            {
                product.CatalogueId = SelectCatalogueDropDownList1.SelectedCatalogueId;
                product.ProductName = ProductNameTextBox.Text;
                product.ProductCode = ProductCodeTextBox.Text;
                product.ProductContent = ProductContentFCKeditor.Text;
                product.IsRecommend = IsRecommendCheckBox.Checked;
                product.ParentPath = SelectCatalogueDropDownList1.SelectedCataloguePath;
                UploadImageAndGetIco(product);
            }
            return product;
        }

        /// <summary>
        /// 上传文件生成缩略图
        /// </summary>
        /// <param name="project"></param>
        private void UploadImageAndGetIco(Product product)
        {
            if (ProductImageFileUpLoad.HasFile)
            {
                string[] paths = UtilityTool.GetNewPicPath(ProductImageFileUpLoad.FileName);
                string physicalPath = paths[1];
                ProductImageFileUpLoad.PostedFile.SaveAs(physicalPath);

                product.ProductImage = paths[0];

                try
                {
                    //生成缩略图
                    string[] thumImagePaths = UtilityTool.GetIcoPicPath(paths[0]);
                    string thumbImagePhysicalPath = thumImagePaths[1];

                    productLogic.GetThumbnailImage(physicalPath, thumbImagePhysicalPath);
                    product.ProductIco = thumImagePaths[0];
                }
                catch
                {
                    product.ProductIco = paths[0];
                }
            }
        }

        protected override void InitLogic()
        {
            productLogic = new ProductLogic();
            catalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            if (productLogic != null)
            {
                productLogic.Dispose();
                productLogic = null;
            }
            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
        }

    }
}